package github.leavesczy.reactivehttpsamples.core.viewmodel.main

import androidx.lifecycle.MutableLiveData
import github.leavesczy.reactivehttpsamples.base.BaseViewModel
import github.leavesczy.reactivehttpsamples.core.mode.MeasureFileSerialNumData
import github.leavesczy.reactivehttpsamples.core.mode.MeasureModelNumData
import github.leavesczy.reactivehttpsamples.util.FileUtilHelper
import github.leavesczy.reactivehttpsamples.util.Logger
import okhttp3.RequestBody
import java.io.File

class HomeViewModel:BaseViewModel() {

    val addMeasureModelData=MutableLiveData<Any>()
    val getMeasureModelNumData=MutableLiveData<MeasureModelNumData>()
    val measureFileSerialNumData=MutableLiveData<MeasureFileSerialNumData>()


    fun uploadFile(file: File, fileName: String, bridgeCode: String, bridgeName: String,sampleType:Int) {
        remoteDataSource.enqueueLoading({
         val filePart=  FileUtilHelper.prepareFilePart("file",file);
            val otherParams= mutableMapOf<String,RequestBody>()
            FileUtilHelper.putRequestBodyMap(otherParams,"bridgeCode",bridgeCode)
            FileUtilHelper.putRequestBodyMap(otherParams,"bridgeName",bridgeName)
            FileUtilHelper.putRequestBodyMap(otherParams,"fileName",fileName)
            FileUtilHelper.putRequestBodyMap(otherParams,"sampleType",sampleType.toString())
            Logger.d("uploadFile-->组装参数，开始调用")
            requireAddMeasureModel(filePart,otherParams)
        }){
            onSuccess {
                Logger.d("uploadFile-->调用成功")
                addMeasureModelData.value=it
            }
            onFailed {e->
                Logger.d("uploadFile-->调用失败"+e.errorMessage)
            }
            onFinally {
                Logger.d("uploadFile-->调用结束")
            }
        }
    }

    fun requestRefreshMeasureNum(bridgeCode: String) {

        remoteDataSource.enqueueLoading({
            val map= mutableMapOf<String,Any>()
            map.put("bridgeCode",bridgeCode)
            requireGetMeasureModelNum(createJsonBody(map))
        }){
            onSuccess {
                getMeasureModelNumData.value=it
            }
        }
    }

    fun requestMeasureFileSerialNum(bridgeCode: String, sampleType: Int) {

        remoteDataSource.enqueueLoading({
            val map= mutableMapOf<String,Any>()
            map.put("bridgeCode",bridgeCode)
            map.put("sampleType",sampleType)
            requestMeasureFileSerialNum(createJsonBody(map))
        }){
            onSuccess {
                measureFileSerialNumData.value=it
            }
        }
    }
}